//class ComboBox
package baidu.ui.controls
{
    import baidu.ui.controls.list.*;
    import baidu.ui.core.*;
    import baidu.ui.data.*;
    import baidu.ui.events.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;

    public class ComboBox extends baidu.ui.core.BUI
    {
        public function ComboBox()
        {
            super();
            return;
        }

        public override function get classStyles():Object
        {
            return mergeStyles(super.classStyles, defaultStyles);
        }

        public override function get enabled():Boolean
        {
            return super.enabled;
        }

        public override function set enabled(arg1:Boolean):void
        {
            super.enabled = arg1;
            mouseEnabled = arg1;
            this.background.enabled = arg1;
            if (!arg1)
            {
                this.close();
            }
            return;
        }

        public function get rowCount():int
        {
            return this._rowCount;
        }

        public function set rowCount(arg1:int):void
        {
            this._rowCount = Math.max(1, arg1);
            invalidate(baidu.ui.core.Invalidation.SIZE);
            return;
        }

        public function get selectedIndex():int
        {
            return this.list.selectedIndex;
        }

        public function set selectedIndex(arg1:int):void
        {
            this.list.selectedIndex = arg1;
            invalidate(baidu.ui.core.Invalidation.SELECTED);
            return;
        }

        public function get selectedItem():Object
        {
            return this.list.selectedItem;
        }

        public function set selectedItem(arg1:Object):void
        {
            this.list.selectedItem = arg1;
            invalidate(baidu.ui.core.Invalidation.SELECTED);
            return;
        }

        public function get dropdown():baidu.ui.controls.List
        {
            return this.list;
        }

        public function get length():int
        {
            return this.list.length;
        }

        public function get dataProvider():baidu.ui.data.DataProvider
        {
            return this.list.dataProvider;
        }

        public function set dataProvider(arg1:baidu.ui.data.DataProvider):void
        {
            arg1.addEventListener(baidu.ui.events.DataChangeEvent.DATA_CHANGE, this.handleDataChange, false, 0, true);
            this.list.dataProvider = arg1;
            invalidate(baidu.ui.core.Invalidation.DATA);
            return;
        }

        public function get dropdownWidth():Number
        {
            return this.list.width;
        }

        public function set dropdownWidth(arg1:Number):void
        {
            this._dropdownWidth = arg1;
            invalidate(baidu.ui.core.Invalidation.SIZE);
            return;
        }

        public function addItem(arg1:Object):void
        {
            this.list.addItem(arg1);
            invalidate(baidu.ui.core.Invalidation.DATA);
            return;
        }

        public function addItemAt(arg1:Object, arg2:uint):void
        {
            this.list.addItemAt(arg1, arg2);
            invalidate(baidu.ui.core.Invalidation.DATA);
            return;
        }

        public function removeAll():void
        {
            this.list.removeAll();
            this.textField.text = "";
            invalidate(baidu.ui.core.Invalidation.DATA);
            return;
        }

        public function removeItem(arg1:Object):Object
        {
            return this.list.removeItem(arg1);
        }

        public function removeItemAt(arg1:uint):void
        {
            this.list.removeItemAt(arg1);
            invalidate(baidu.ui.core.Invalidation.DATA);
            return;
        }

        public function getItemAt(arg1:uint):Object
        {
            return this.list.getItemAt(arg1);
        }

        public function replaceItemAt(arg1:Object, arg2:uint):Object
        {
            return this.list.replaceItemAt(arg1, arg2);
        }

        public function sortItems(... rest):*
        {
            return this.list.sortItems.apply(this.list, rest);
        }

        public function sortItemsOn(arg1:String, arg2:Object=null):*
        {
            return this.list.sortItemsOn(arg1, arg2);
        }

        public function open():void
        {
            if (this.isOpen || this.length == 0)
            {
                return;
            }
            this.isOpen = true;
            if (stage)
            {
                stage.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onStageClick, false, 0, true);
                this.adjustListPosition();
                this.list.scrollToSelected();
                stage.addChild(this.list);
            }
            dispatchEvent(new flash.events.Event(flash.events.Event.OPEN));
            return;
        }

        public function close():void
        {
            if (!this.isOpen)
            {
                return;
            }
            if (stage)
            {
                stage.removeEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onStageClick);
                this.isOpen = false;
                if (stage.contains(this.list))
                {
                    stage.removeChild(this.list);
                }
            }
            dispatchEvent(new flash.events.Event(flash.events.Event.CLOSE));
            return;
        }

        protected override function initUI():void
        {
            super.initUI();
            setSize(100, 22);
            this.background = new baidu.ui.controls.Button();
            this.background.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onToggleListVisibility, false, 0, true);
            addChild(this.background);
            this.textField = new flash.text.TextField();
            addChild(this.textField);
            this.textField.mouseEnabled = false;
            this.list = new baidu.ui.controls.List();
            this.list.dataProvider.addEventListener(baidu.ui.events.DataChangeEvent.DATA_CHANGE, this.handleDataChange, false, 0, true);
            this.list.addEventListener(flash.events.Event.CHANGE, this.onListChange, false, 0, true);
            this.list.addEventListener(baidu.ui.events.ListEvent.ITEM_CLICK, this.onListChange, false, 0, true);
            this.list.addEventListener(baidu.ui.events.ListEvent.ITEM_ROLL_OUT, this.passEvent, false, 0, true);
            this.list.addEventListener(baidu.ui.events.ListEvent.ITEM_ROLL_OVER, this.passEvent, false, 0, true);
            this.list.scrollPane.vScrollBar.addEventListener(flash.events.Event.SCROLL, this.passEvent, false, 0, true);
            return;
        }

        protected function handleDataChange(arg1:baidu.ui.events.DataChangeEvent):void
        {
            var loc1:*;
            loc1 = this.selectedIndex;
            if (this.dataProvider.length != 0)
            {
                loc1 = Math.max(0, Math.min(loc1, (this.length - 1)));
            }
            else
            {
                loc1 = Math.max(-1, Math.min(loc1, (this.length - 1)));
            }
            if (this.list.selectedIndex != loc1)
            {
                this.list.selectedIndex = loc1;
                invalidate(baidu.ui.core.Invalidation.SELECTED, false);
            }
            invalidate(baidu.ui.core.Invalidation.DATA);
            return;
        }

        protected override function draw():void
        {
            var loc1:*;
            loc1 = undefined;
            if (isInvalid(baidu.ui.core.Invalidation.STYLES))
            {
                invalidate(baidu.ui.core.Invalidation.SIZE, false);
                loc1 = getSkinInstance(getStyleValue("skin"));
                this.background.setStyle("skin", loc1);
            }
            if (isInvalid(baidu.ui.core.Invalidation.SIZE, baidu.ui.core.Invalidation.DATA, baidu.ui.core.Invalidation.STATE))
            {
                this.drawTextFormat();
                this.drawLayout();
                invalidate(baidu.ui.core.Invalidation.DATA);
            }
            if (isInvalid(baidu.ui.core.Invalidation.DATA))
            {
                this.drawList();
                invalidate(baidu.ui.core.Invalidation.SELECTED, true);
            }
            if (isInvalid(baidu.ui.core.Invalidation.SELECTED))
            {
                if (this.selectedIndex != -1)
                {
                    this.textField.text = this.list.selectedItem.label;
                }
                else
                {
                    this.textField.text = "";
                }
            }
            super.draw();
            return;
        }

        protected function drawLayout():void
        {
            var loc1:*;
            loc1 = getStyleValue("buttonWidth") as Number;
            var loc2:*;
            loc2 = getStyleValue("textPadding") as Number;
            this.background.setSize(_width, _height);
            var loc3:*;
            this.textField.y = loc3 = loc2;
            this.textField.x = loc3;
            this.textField.width = _width - loc1 - loc2;
            this.textField.height = _height - loc2;
            this.list.width = isNaN(this._dropdownWidth) ? _width : this._dropdownWidth;
            this.background.enabled = this.enabled;
            this.background.drawNow();
            return;
        }

        protected function drawTextFormat():void
        {
            var loc1:*;
            loc1 = getStyleValue(_enabled ? "textFormat" : "disabledTextFormat") as flash.text.TextFormat;
            if (loc1 == null)
            {
                loc1 = new flash.text.TextFormat();
            }
            this.textField.defaultTextFormat = loc1;
            this.textField.setTextFormat(loc1);
            return;
        }

        protected function drawList():void
        {
            this.list.rowCount = Math.max(0, Math.min(this._rowCount, this.list.dataProvider.length));
            return;
        }

        protected function adjustListPosition():void
        {
            var loc1:*;
            loc1 = localToGlobal(new flash.geom.Point(0, 0));
            this.list.x = loc1.x;
            if (loc1.y + height + this.list.height > stage.stageHeight)
            {
                this.list.y = loc1.y - this.list.height;
            }
            else
            {
                this.list.y = loc1.y + height;
            }
            return;
        }

        protected function onToggleListVisibility(arg1:flash.events.MouseEvent):void
        {
            arg1.stopPropagation();
            dispatchEvent(arg1);
            if (this.isOpen)
            {
                this.close();
            }
            else
            {
                this.open();
                stage.addEventListener(flash.events.MouseEvent.MOUSE_UP, this.onListItemUp, false, 0, true);
            }
            return;
        }

        protected function onListItemUp(arg1:flash.events.MouseEvent):void
        {
            stage.removeEventListener(flash.events.MouseEvent.MOUSE_UP, this.onListItemUp);
            if (!(arg1.target as baidu.ui.controls.list.IListCell) || !this.list.contains(arg1.target as flash.display.DisplayObject))
            {
                return;
            }
            var loc1:*;
            loc1 = this.selectedIndex;
            this.selectedIndex = arg1.target.listData.index;
            if (loc1 != this.selectedIndex)
            {
                dispatchEvent(new flash.events.Event(flash.events.Event.CHANGE));
            }
            this.close();
            return;
        }

        protected function onListChange(arg1:flash.events.Event):void
        {
            dispatchEvent(arg1);
            invalidate(baidu.ui.core.Invalidation.SELECTED);
            this.close();
            return;
        }

        protected function onStageClick(arg1:flash.events.MouseEvent):void
        {
            if (!this.isOpen)
            {
                return;
            }
            if (!contains(arg1.target as flash.display.DisplayObject) && !this.list.contains(arg1.target as flash.display.DisplayObject))
            {
                this.close();
            }
            return;
        }

        protected function passEvent(arg1:flash.events.Event):void
        {
            dispatchEvent(arg1);
            return;
        }


        {
            defaultStyles = {"skin":"ComboBox_Skin", "buttonWidth":20, "textPadding":2};
        }

        protected var textField:flash.text.TextField;

        protected var background:baidu.ui.controls.Button;

        public var list:baidu.ui.controls.List;

        protected var isOpen:Boolean=false;

        protected var _rowCount:uint=5;

        protected var _dropdownWidth:Number;

        public static var defaultStyles:Object;
    }
}